package com.example.wenliangjiudian.service.impl;

import com.example.wenliangjiudian.entity.Customer;
import com.example.wenliangjiudian.mapper.CustomerMapper;
import com.example.wenliangjiudian.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public void save(Customer customer) {
        if (customer.getIdcard() == null) {
            return;
        }

        // 1。先根据 身份证号  查询  是否有这个人
        Customer aaa = customerMapper.getByIdcard(customer.getIdcard());

        // 2. 如果没有  就新增
        //3. 如果有 就更新
        if (aaa == null) {
            customerMapper.insert(customer);
        } else {
            customer.setId(aaa.getId());
            customerMapper.update(customer);
        }

    }

    @Override
    public List<Customer> selectAll() {
        List<Customer> customers = customerMapper.selectAll();
        return customers;
    }

    @Override
    public Customer getByIdcard(String idcard) {
        return customerMapper.getByIdcard(idcard);
    }
}
